package com.dodayum.dubbo;

import com.alibaba.dubbo.common.Constants;
import com.alibaba.dubbo.common.URL;
import com.alibaba.dubbo.registry.NotifyListener;
import com.alibaba.dubbo.registry.RegistryService;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Author DoDayum
 * @Date 2024-01-24-21:32
 */
@Service
public class MyRegistry implements RegistryService {

    /*
    URL里的数据
    {
        "absolutePath": "/com.dodayum.service.UserService",
        "address": "192.168.31.182:20880",
        "anyHost": true,
        "backupAddress": "192.168.31.182:20880",
        "backupUrls": [
            {
                "$ref": "$"
            }
        ],
        "host": "192.168.31.182",
        "localHost": false,
        "parameters": {
            "side": "provider",
            "application": "user-service-provider",
            "methods": "sayHelloDemo,sayHello",
            "dubbo": "2.0.2",
            "pid": "71380",
            "interface": "com.dodayum.service.UserService",
            "generic": "false",
            "timeout": "3000",
            "anyhost": "true",
            "bean.name": "com.dodayum.service.UserService",
            "timestamp": "1706940410447"
        },
        "path": "com.dodayum.service.UserService",
        "port": 20880,
        "protocol": "dubbo",
        "serviceInterface": "com.dodayum.service.UserService",
        "serviceKey": "com.dodayum.service.UserService",
        "serviceName": "com.dodayum.service.UserService"
    }
     */

    /**
     * 注册
     *
     * @param url Registration information , is not allowed to be empty, e.g: dubbo://10.20.153.10/com.alibaba.foo.BarService?version=1.0.0&application=kylin
     */
    @Override
    public void register(URL url) {
        this.printInputInfo(url);
        UrlList.addUrl(url);
    }

    /**
     * 取消注册
     *
     * @param url Registration information , is not allowed to be empty, e.g: dubbo://10.20.153.10/com.alibaba.foo.BarService?version=1.0.0&application=kylin
     */
    @Override
    public void unregister(URL url) {
        this.printInputInfo(url);
    }

    /**
     * 订阅
     *
     * @param url            Subscription condition, not allowed to be empty, e.g. consumer://10.20.153.10/com.alibaba.foo.BarService?version=1.0.0&application=kylin
     * @param notifyListener A listener of the change event, not allowed to be empty
     */
    @Override
    public void subscribe(URL url, NotifyListener notifyListener) {
        this.printInputInfo(url);
    }

    /**
     * 取消订阅
     *
     * @param url            Subscription condition, not allowed to be empty, e.g. consumer://10.20.153.10/com.alibaba.foo.BarService?version=1.0.0&application=kylin
     * @param notifyListener A listener of the change event, not allowed to be empty
     */
    @Override
    public void unsubscribe(URL url, NotifyListener notifyListener) {
        this.printInputInfo(url);
    }

    /**
     * 查询
     *
     * @param url Query condition, is not allowed to be empty, e.g. consumer://10.20.153.10/com.alibaba.foo.BarService?version=1.0.0&application=kylin
     * @return urls
     */
    @Override
    public List<URL> lookup(URL url) {
        this.printInputInfo(url);
        return null;
    }

    /**
     * 标记使用
     * @param url dubbo的url协议
     */
    private void printInputInfo(URL url) {
        System.out.println("*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=start");
        // 当前角色
        String role = url.getParameter(Constants.SIDE_KEY);
        // 进入的是哪个方法
        String methodName = "null";
        StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
        String classname = this.getClass().getCanonicalName();
        for (StackTraceElement stackTraceElement : stackTrace) {
            if (!ObjectUtils.isEmpty(stackTraceElement) && !StringUtils.isEmpty(stackTraceElement.getClassName())
                    && stackTraceElement.getClassName().equals(classname)) {
                if (!"printInputInfo".equals(stackTraceElement.getMethodName())) {
                    methodName = stackTraceElement.getMethodName();
                    break;
                }
            }
        }
        System.out.println("当前角色:\t" + role + "\t当前方法:\t" + methodName + "\nurl:\t" + url + "\nJSON格式化后:\t" + JSON.toJSONString(url));
        System.out.println("*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=stop");

    }
}
